#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include "ymodem.h"


// 声明一个全局的 QSerialPort 对象
QSerialPort serial;

char ser_buff[1024*12] ;
long ser_len= 0;
int getData(char* data, unsigned int len, unsigned int mstime) {
    if (!serial.isOpen()) {
        qWarning() << "Serial port is not open.";
        return -1;
    }

    if(ser_len >= len )
    {
        memcpy(data, ser_buff, len);
        ser_len -= len;
        qWarning() << len;
        return len;
    }

    if (!serial.waitForReadyRead(mstime)) {
        qWarning() << "Read timeout.";
        return -1;
    }


    // QByteArray readData = serial.read(len);
    QByteArray readData = serial.readAll();
    int bytesRead = readData.size();

    memcpy(&ser_buff[ser_len],readData.data(),bytesRead);
    ser_len += bytesRead;

    if (ser_len >= len) {
        memcpy(data, readData.data(), len);
        ser_len -= len;
        memcpy(&ser_buff[0],&ser_buff[len],ser_len);
    }
    else
    {
        return -1;
    }

    qWarning() << len;

    return len;
}



int putData(char* data, unsigned int len, unsigned int mstime) {
    if (!serial.isOpen()) {
        qWarning() << "Serial port is not open.";
        return -1;
    }

    QByteArray writeData(data, len);
    qint64 bytesWritten = serial.write(writeData);

    if (bytesWritten == -1) {
        qWarning() << "Write failed.";
        return -1;
    }

    if (!serial.waitForBytesWritten(mstime)) {
        qWarning() << "Write timeout.";
        return -1;
    }
    qWarning() << writeData.data();
    return static_cast<int>(bytesWritten);
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    // 设置串口参数
    serial.setPortName("COM7");
    serial.setBaudRate(QSerialPort::Baud115200);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);

    if (!serial.open(QIODevice::ReadWrite)) {
        qDebug() << "Failed to open port" << serial.portName();
        return -1;
    }else
    {
        qDebug() << "open port" << serial.portName();
    }

    // 示例使用
    // char sendData[] = "Hello, Serial Port!";
    // char receiveData[10];

    // int bytesSent = putData(sendData, strlen(sendData), 5000);
    // if (bytesSent > 0) {
    //     qDebug() << "Bytes sent:" << bytesSent;
    // } else {
    //     qWarning() << "Failed to send data.";
    // }

    // int bytesReceived = getData(receiveData, sizeof(receiveData), 5000);
    // if (bytesReceived > 0) {
    //     qDebug() << "Bytes received:" << bytesReceived;
    //     qDebug() << "Data received:" << QByteArray(receiveData, bytesReceived);
    // } else {
    //     qWarning() << "Failed to receive data.";
    // }

    ymodem_register(putData,getData);

    char buff[1024*502] =" {1,2,3}";
    // int ret = ymodem_send(buff,sizeof(buff), "2.txt");
    char name[128] = {};
    int ret = ymodem_recv(buff,sizeof(buff), name);

    qWarning() << "code is over."<<ret;
    // 程序退出前关闭串口
    serial.close();

    return 0;
    // int result = a.exec();

}
